package com.ynu.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ynu.bean.PageBean;
import com.ynu.bean.query.UserQuery;
import com.ynu.converter.UserConverter;
import com.ynu.bean.entity.User;
import com.ynu.mapper.UserMapper;
import com.ynu.service.UserService;
import com.ynu.bean.vo.UserVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 *  模块名称：
 *  模块类型：
 *  编码人：迟宽席
 *  创建时间：2023/2/23
 *  联系电话：18388068273
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Resource
    private UserConverter userConverter;

    @Override
    public PageBean<UserVO> getUserByConditionWithPage(UserQuery userQuery) {
        Page<User> page = null;
        Integer pageNum = userQuery.getPageNum();
        Integer pageSize = userQuery.getPageSize();
        if (pageNum != null && pageSize != null) {
            page = PageHelper.startPage(pageNum, pageSize);
        }

        userMapper.selectUserByCondition(userQuery);
        PageBean<User> userPageBean = PageBean.trans(page);
        //将PageBean<User>转换成PageBean<UserVO>
        PageBean<UserVO> userVOPageBean = userConverter.userPageBean2UserVOPageBean(userPageBean);

        return userVOPageBean;
    }


    @Override
    public User getUserById(Long id) {
        User user = userMapper.selectUserById(id);
        return user;
    }


    @Override
    public boolean insertUser(User user) {
        int res = userMapper.insertUser(user);
        return res == 1;
    }

    @Override
    public boolean updateUserSensitiveById(User user) {
        int res = userMapper.updateUserSensitive(user);
        return res == 1;
    }

    @Override
    public boolean deleteUserById(long id) {
        int res = userMapper.deleteUserById(id);
        return res == 1;
    }
}
